package com.tulingxueyuan.mall.modules.pms.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tulingxueyuan.mall.common.api.CommonPage;
import com.tulingxueyuan.mall.common.api.CommonResult;
import com.tulingxueyuan.mall.modules.pms.model.dto.PmsBrandParamDTO;
import com.tulingxueyuan.mall.modules.pms.service.PmsBrandService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 品牌表 前端控制器
 * </p>
 *
 * @author zhaofuhao
 * @since 2022-02-24
 */
@RestController
@RequestMapping("/brand")
public class PmsBrandController {

    @Autowired
    private PmsBrandService pmsBrandService;
    /**
     *     品牌数据列表
     *     在商品分类中进行共用
     *     url:'/brand/list',
     *     method:'get',
     *     params:params
     */
    @ApiOperation(value = "获取全部品牌列表")
    @GetMapping(value = "/list")
    public CommonResult list(@RequestParam(value = "keyword",defaultValue = "") String keyword,
                             @RequestParam(value = "pageNum",defaultValue  ="1")  Integer pageNum,
                             @RequestParam(value = "pageSize",defaultValue  ="5")  Integer pageSize){
      Page page = pmsBrandService.list(keyword,pageNum,pageSize);
      return CommonResult.success(CommonPage.restPage(page));
    }

    /**
     *  url:'/brand/create',
     *  method:'post',
     *   data:data
     */
    @ApiOperation(value = "添加品牌")
    @PostMapping(value = "/create")
    @ResponseBody
    public CommonResult create( @RequestBody PmsBrandParamDTO pmsBrand) {

        boolean count = pmsBrandService.createBrand(pmsBrand);
        if (count) {
          return   CommonResult.success(count);
        } else {
           return CommonResult.failed();
        }

    }
    @ApiOperation(value = "删除品牌")
    @GetMapping("/delete/{id}")
    @ResponseBody
    @PreAuthorize("hasAuthority('pms:brand:delete')")
    public CommonResult delete(@PathVariable("id") Long id) {
        boolean count = pmsBrandService.deleteBrand(id);
        if (count) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }
    @ApiOperation(value = "根据编号查询品牌信息")
    @GetMapping(value = "/{id}")
    @ResponseBody
    public CommonResult getItem(@PathVariable("id") Long id) {
        return CommonResult.success(pmsBrandService.getById(id));
    }
    @ApiOperation(value = "更新品牌")
    @PostMapping(value = "/update/{id}")
    @ResponseBody
    public CommonResult update(@PathVariable("id") Long id,
                               @Validated @RequestBody PmsBrandParamDTO pmsBrand) {

        boolean count = pmsBrandService.updateById(pmsBrand);
        if (count) {
          return   CommonResult.success(count);
        } else {
          return   CommonResult.failed();
        }

    }

    @ApiOperation(value = "批量更新显示状态")
    @PostMapping(value = "/update/showStatus")
    @ResponseBody

    public CommonResult updateShowStatus(@RequestParam("ids") List<Long> ids,
                                         @RequestParam("showStatus") Integer showStatus) {
        boolean count = pmsBrandService.updateShowStatus(ids, showStatus);
        if (count) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }
    /**
     *     url:'/brand/update/factoryStatus',
     *     method:'post',
     *     data:data
     */
    @ApiOperation(value = "批量更新厂家制造商状态")
    @PostMapping("/update/factoryStatus")
    @ResponseBody
    @PreAuthorize("hasAuthority('pms:brand:update')")
    public CommonResult updateFactoryStatus(@RequestParam("ids") List<Long> ids,
                                            @RequestParam("factoryStatus") Integer factoryStatus){
        boolean count = pmsBrandService.updateFactoryStatus(ids, factoryStatus);
        if (count) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }

    }


}

